<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>nginx | 学习笔记</title><meta name="author" content="lzoxun"><meta name="copyright" content="lzoxun"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="开启 nginx安装下载 1234567891011121314151617181920212223242526272829303132333435363738# 版本Nginx 开源版# 在开源版上添加扩展很多功能Nginx Plus 商业版 https:&#x2F;&#x2F;www.nginx.comOpenresty 其他开源版 http:&#x2F;&#x2F;openresty.orgtengine 其他开源版 http:&#x2F;">
<meta property="og:type" content="article">
<meta property="og:title" content="nginx">
<meta property="og:url" content="http://example.com/2023/07/22/linux/linux-nginx/index.html">
<meta property="og:site_name" content="学习笔记">
<meta property="og:description" content="开启 nginx安装下载 1234567891011121314151617181920212223242526272829303132333435363738# 版本Nginx 开源版# 在开源版上添加扩展很多功能Nginx Plus 商业版 https:&#x2F;&#x2F;www.nginx.comOpenresty 其他开源版 http:&#x2F;&#x2F;openresty.orgtengine 其他开源版 http:&#x2F;">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png">
<meta property="article:published_time" content="2023-07-22T07:14:09.680Z">
<meta property="article:modified_time" content="2023-07-22T07:14:09.680Z">
<meta property="article:author" content="lzoxun">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/2023/07/22/linux/linux-nginx/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"top_n_per_article":1,"unescape":true,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#1f1f1f","position":"bottom-left"},
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'nginx',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-07-22 15:14:09'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          const now = new Date()
          const hour = now.getHours()
          const isNight = hour <= 6 || hour >= 18
          if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
          else if (t === 'light') activateLightMode()
          else activateDarkMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>(()=>{
  const $loadingBox = document.getElementById('loading-box')
  const $body = document.body
  const preloader = {
    endLoading: () => {
      $body.style.overflow = ''
      $loadingBox.classList.add('loaded')
    },
    initLoading: () => {
      $body.style.overflow = 'hidden'
      $loadingBox.classList.remove('loaded')
    }
  }

  preloader.initLoading()
  window.addEventListener('load',() => { preloader.endLoading() })

  if (false) {
    document.addEventListener('pjax:send', () => { preloader.initLoading() })
    document.addEventListener('pjax:complete', () => { preloader.endLoading() })
  }
})()</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">108</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">1</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header"><nav id="nav"><span id="blog-info"><a href="/" title="学习笔记"><span class="site-name">学习笔记</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">nginx</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-07-22T07:14:09.680Z" title="发表于 2023-07-22 15:14:09">2023-07-22</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-07-22T07:14:09.680Z" title="更新于 2023-07-22 15:14:09">2023-07-22</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="nginx"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="开启-nginx"><a href="#开启-nginx" class="headerlink" title="开启 nginx"></a>开启 nginx</h2><h3 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h3><p><a target="_blank" rel="noopener" href="http://nginx.org/en/download.html">下载</a></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">版本</span></span><br><span class="line">Nginx 开源版</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">在开源版上添加扩展很多功能</span></span><br><span class="line">Nginx Plus 商业版 https://www.nginx.com</span><br><span class="line">Openresty 其他开源版 http://openresty.org</span><br><span class="line">tengine 其他开源版 http://tengine.taobao.org</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">开源版</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">随便找个位置</span></span><br><span class="line">wget http://nginx.org/download/nginx-1.24.0.tar.gz</span><br><span class="line">tar -xf http://nginx.org/download/nginx-1.24.0.tar.gz</span><br><span class="line">cd nginx-1.24.0</span><br><span class="line">chmod +x configure</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">依赖环境</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">c编译器</span></span><br><span class="line">yum -y install gcc gcc-c++ autoconf automake make</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">解析正则的pcre库</span></span><br><span class="line">yum install -y pcre pcre-devel</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">添加对gzip的支持</span></span><br><span class="line">yum install -y zlib zlib-devel</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">SSL</span></span><br><span class="line">yum -y install pcre  pcre-devel zlib  zlib-devel openssl openssl-devel</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">开始安装</span></span><br><span class="line">./configure --with-stream # 同时安装stream模块，后期处理tcp转发</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">其他参数</span></span><br><span class="line">--prefix=/usr/local/nginx # 指定安装目录，默认就是这里</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">安装完成之后编译</span></span><br><span class="line">make</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">安装编译后的文件（成功后位置 `/usr/local/nginx `或`/usr/share/nginx`）</span></span><br><span class="line">make install</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">将ng的sbin位置也添加到PATH环境变量中</span></span><br><span class="line">export PATH=&quot;/usr/local/nginx/sbin/:$PATH&quot;</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">查看版本</span></span><br><span class="line">nginx -v</span><br></pre></td></tr></table></figure>
<h3 id="配置-systemctl"><a href="#配置-systemctl" class="headerlink" title="配置 systemctl"></a>配置 systemctl</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">创建 nginx.service</span></span><br><span class="line">vim /lib/systemd/system/nginx.service</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">配置内容</span></span><br><span class="line">[Unit]</span><br><span class="line">Description=nginx service   # 描述服务</span><br><span class="line">After=network.target  # 描述服务类别</span><br><span class="line"></span><br><span class="line">[Service]</span><br><span class="line">Type=forking  # 是后台运行的形式</span><br><span class="line">ExecStart=/usr/local/nginx/sbin/nginx         # nginx的可执行路径</span><br><span class="line">ExecReload=/usr/local/nginx/sbin/nginx -s reload </span><br><span class="line">ExecStop=/usr/local/nginx/sbin/nginx -s stop</span><br><span class="line">ExecQuit=/usr/local/nginx/sbin/nginx -s quit # 优雅关闭，连接的存在工作中的，等待完成再关闭</span><br><span class="line">PrivateTmp=true # 表示给服务分配独立的临时空间</span><br><span class="line"></span><br><span class="line">[Install]</span><br><span class="line">WantedBy=multi-user.target  # 运行级别 自启</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">测试</span></span><br><span class="line">systemctl start nginx.service # 启动</span><br><span class="line">systemctl list-unit-files | grep nginx #查看是否自启</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">将/usr/local/nginx 软连接到 /etc下</span></span><br><span class="line">ln -s /usr/local/nginx /etc/nginx</span><br></pre></td></tr></table></figure>

<h3 id="添加模块"><a href="#添加模块" class="headerlink" title="添加模块"></a>添加模块</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">查看当时安装时的参数</span></span><br><span class="line">nginx -V</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">进入nginx 安装包位置，重新安装</span></span><br><span class="line">./configure --with-stream --with-http_ssl_module</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">make 编译，不要make install，不然会覆盖你的nginx目录</span></span><br><span class="line">make</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">备份 /usr/local/nginx/sbin/nginx</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">nginx-1.24.x/objs 下的 nginx 覆盖到 /usr/local/nginx/sbin/</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h3 id="nginx源"><a href="#nginx源" class="headerlink" title="nginx源"></a>nginx源</h3>  <figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">安装 yum-utils，此插件可让我们自主选择 yum 源</span></span><br><span class="line">yum install yum-utils -y</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">添加</span></span><br><span class="line">vim /etc/yum.repos.d/nginx.repo</span><br><span class="line">[nginx-stable]</span><br><span class="line">name=nginx stable repo</span><br><span class="line">baseurl=http://nginx.org/packages/centos/$releasever/$basearch/</span><br><span class="line">gpgcheck=0</span><br><span class="line">enabled=1</span><br><span class="line">module_hotfixes=true</span><br><span class="line"></span><br><span class="line">[nginx-mainline]</span><br><span class="line">name=nginx mainline repo</span><br><span class="line">baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/</span><br><span class="line">gpgcheck=0</span><br><span class="line">enabled=0</span><br><span class="line">module_hotfixes=true</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">切换 yum 源为 Nginx 稳定版本的 yum 源</span></span><br><span class="line">yum-config-manager --enable nginx-stable</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">查看yum上的Nginx版本</span></span><br><span class="line">yum info nginx</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">安装</span></span><br><span class="line">yum install nginx -y </span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">启动服务，关闭防火墙或开发80端口，与服务器还需要去安全组开放80端口</span></span><br><span class="line">xx.xx.xx.xx:80 </span><br></pre></td></tr></table></figure>
<h3 id="目录结构"><a href="#目录结构" class="headerlink" title="目录结构"></a>目录结构</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">conf  # 配置文件</span><br><span class="line">html  # 默认静态文件</span><br><span class="line">logs  # 日志</span><br><span class="line"><span class="meta prompt_">	  # </span><span class="language-bash">每个人的范围都会写到日志里，无限扩增，一定大小会写到其他文件，如果磁盘满了，就会有各种问题</span></span><br><span class="line">sbin  # 执行指令</span><br></pre></td></tr></table></figure>

<h3 id="访问"><a href="#访问" class="headerlink" title="访问"></a>访问</h3><p>正常情况下只有本机才能范围nginx开启的服务，局域网内其他设备无法访问到的</p>
<p>其他设备只能访问到本机，访问不到本机上的虚拟机</p>
<p>解决方案</p>
<ul>
<li>本机window上安装nginx，其他设备访问本机，通过nginx代理到虚拟机的服务中</li>
</ul>
<h2 id="配置"><a href="#配置" class="headerlink" title="配置"></a>配置</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">配置文件解析</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">第一部分全局配置</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">作用:从文件开始到events直接的部分，主要处理一些影响nginx服务器整体运行的配置指令</span></span><br><span class="line">worker_processes  1; # nginx处理并发的数量（一般 最多设置内核线程数）</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">error_log  logs/error.log;</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">error_log  logs/error.log  notice;</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">error_log  logs/error.log  info;</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">pid        logs/nginx.pid;</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">第二部事件配置</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">作用:里面的指令主要音响nginx服务器与用用户的网络链接</span></span><br><span class="line">events &#123;</span><br><span class="line">    worker_connections  1024; # 最大链接数目</span><br><span class="line">&#125;</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">第三部分http配置</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">包括http全局块、server块</span></span><br><span class="line">http &#123;</span><br><span class="line">    include       mime.types; # 引入其他文件（用于服务器告诉客户端，响应的数据是什么类型，可以自己加）</span><br><span class="line">    default_type  application/octet-stream;# mime.types 中没有的类型 就用这个默认类型</span><br><span class="line"></span><br><span class="line">    #log_format  main  &#x27;$remote_addr - $remote_user [$time_local] &quot;$request&quot; &#x27;</span><br><span class="line">    #                  &#x27;$status $body_bytes_sent &quot;$http_referer&quot; &#x27;</span><br><span class="line">    #                  &#x27;&quot;$http_user_agent&quot; &quot;$http_x_forwarded_for&quot;&#x27;;</span><br><span class="line"></span><br><span class="line">    #access_log  logs/access.log  main;</span><br><span class="line"></span><br><span class="line">    sendfile        on; # 数据 0 拷贝(用户请求数据，Nginx 不从硬盘复制数据转给用户，直接让操作系统从硬盘把数据发给用户)</span><br><span class="line">    #tcp_nopush     on;</span><br><span class="line"></span><br><span class="line">    keepalive_timeout  65;</span><br><span class="line"></span><br><span class="line">    #gzip  on;</span><br><span class="line">    # 负载均衡配置 一</span><br><span class="line">    # 测试:http://192.168.1.104/vod/index.html 刷新会按顺序获取包含服务的相应页面</span><br><span class="line">    upstream myserver &#123; # 配置分发负载的服务器列表</span><br><span class="line">    	server 192.168.1.104:8000;</span><br><span class="line">    	server 192.168.1.104:8080;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    # 配置虚拟主机相关信息，可以很多个，互不干扰(重要)</span><br><span class="line">    server &#123;</span><br><span class="line">        listen       80; # 防火墙要开通80端口</span><br><span class="line">        server_name  192.168.1.104;</span><br><span class="line"></span><br><span class="line">        #charset koi8-r;</span><br><span class="line"></span><br><span class="line">        #access_log  logs/host.access.log  main;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        location / &#123; # 访问路径（URI 资源，不包括域名端口）符合该正则时做的事情</span><br><span class="line">            root   html;</span><br><span class="line">            proxy_pass http://myserver; # 上面定义的名称</span><br><span class="line">            index  index.html index.htm;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        #error_page  404              /404.html;</span><br><span class="line"></span><br><span class="line">        # redirect server error pages to the static page /50x.html</span><br><span class="line">        #</span><br><span class="line">        error_page   500 502 503 504  /50x.html; # 500 502 503 504 这些错误码都会到 50x.html 页面</span><br><span class="line">        location = /50x.html &#123;</span><br><span class="line">            root   html;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        # proxy the PHP scripts to Apache listening on 127.0.0.1:80</span><br><span class="line">        #</span><br><span class="line">        #location ~ \.php$ &#123;</span><br><span class="line">        #    proxy_pass   http://127.0.0.1;</span><br><span class="line">        #&#125;</span><br><span class="line"></span><br><span class="line">        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000</span><br><span class="line">        #</span><br><span class="line">        #location ~ \.php$ &#123;</span><br><span class="line">        #    root           html;</span><br><span class="line">        #    fastcgi_pass   127.0.0.1:9000;</span><br><span class="line">        #    fastcgi_index  index.php;</span><br><span class="line">        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;</span><br><span class="line">        #    include        fastcgi_params;</span><br><span class="line">        #&#125;</span><br><span class="line"></span><br><span class="line">        # deny access to .htaccess files, if Apache&#x27;s document root</span><br><span class="line">        # concurs with nginx&#x27;s one</span><br><span class="line">        #</span><br><span class="line">        #location ~ /\.ht &#123;</span><br><span class="line">        #    deny  all;</span><br><span class="line">        #&#125;</span><br><span class="line">    &#125;</span><br><span class="line">    # 反向代理案例</span><br><span class="line">    # 反向代理转发,访问http://192.168.1.104:9001 时候 根据条件代理到 http://127.0.0.1:8080 和 .....:8000服务器</span><br><span class="line">    server &#123;</span><br><span class="line">        listen       9001;</span><br><span class="line">        server_name  192.168.1.104;</span><br><span class="line">        # = 严格匹配</span><br><span class="line">        # ~ uri包含正则，区分大小写</span><br><span class="line">        # ~* uri包含正则，不区分大小写</span><br><span class="line">        # ~~</span><br><span class="line">        location ~ /edu/ &#123;</span><br><span class="line">            root   html;</span><br><span class="line">	    proxy_pass http://127.0.0.1:8080; #80r800 这个服务下面需要有edu这个文件夹</span><br><span class="line">            index  index.html index.htm;</span><br><span class="line">        &#125;</span><br><span class="line">        location ~ /vod/ &#123;</span><br><span class="line">            root   html;</span><br><span class="line">	    proxy_pass http://127.0.0.1:8000; #8000 这个服务下面需要有vod这个文件夹</span><br><span class="line">            index  index.html index.htm;</span><br><span class="line">	&#125;</span><br><span class="line">    &#125;</span><br><span class="line">    # 动静分离</span><br><span class="line">    server &#123;</span><br><span class="line">        listen       9002;</span><br><span class="line">        server_name  192.168.1.104;</span><br><span class="line">        location /www/ &#123; # 范围路径有www的时候</span><br><span class="line">            root  /data/; # root /data/ 设置为静态资源服务器  , root 要缓存alias</span><br><span class="line">            index  index.html index.htm;</span><br><span class="line">        &#125;</span><br><span class="line">        location /image/ &#123;</span><br><span class="line">            root  /data/;</span><br><span class="line">            autoindex on; # 列出当前文件夹的文件列表</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    # another virtual host using mix of IP-, name-, and port-based configuration</span><br><span class="line">    #</span><br><span class="line">    #server &#123;</span><br><span class="line">    #    listen       8000;</span><br><span class="line">    #    listen       somename:8080;</span><br><span class="line">    #    server_name  somename  alias  another.alias;</span><br><span class="line"></span><br><span class="line">    #    location / &#123;</span><br><span class="line">    #        root   html;</span><br><span class="line">    #        index  index.html index.htm;</span><br><span class="line">    #    &#125;</span><br><span class="line">    #&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    # HTTPS server</span><br><span class="line">    #</span><br><span class="line">    #server &#123;</span><br><span class="line">    #    listen       443 ssl;</span><br><span class="line">    #    server_name  localhost;</span><br><span class="line"></span><br><span class="line">    #    ssl_certificate      cert.pem;</span><br><span class="line">    #    ssl_certificate_key  cert.key;</span><br><span class="line"></span><br><span class="line">    #    ssl_session_cache    shared:SSL:1m;</span><br><span class="line">    #    ssl_session_timeout  5m;</span><br><span class="line"></span><br><span class="line">    #    ssl_ciphers  HIGH:!aNULL:!MD5;</span><br><span class="line">    #    ssl_prefer_server_ciphers  on;</span><br><span class="line"></span><br><span class="line">    #    location / &#123;</span><br><span class="line">    #        root   html;</span><br><span class="line">    #        index  index.html index.htm;</span><br><span class="line">    #    &#125;</span><br><span class="line">    #&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>


<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">vue <span class="built_in">history</span> 模式 ，如果 URL 不匹配任何静态资源，它应提供与你的应用程序中的 index.html 相同的页面。</span></span><br><span class="line">location / &#123;</span><br><span class="line">  try_files $uri $uri/ /index.html;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<hr>
<h3 id="基本概念"><a href="#基本概念" class="headerlink" title="基本概念"></a>基本概念</h3><ul>
<li>nginx 是一个<code>高性能</code>的<code>HTTP</code>和<code>反向代理服务器</code>,<code>占用内存少</code>、<code>并发能力强</code></li>
<li><code>热部署</code>:持续运行，在不关闭服务情况下升级软件版本</li>
<li>nginx -s reload 重新加载配置文件</li>
<li>nginx -t 查看配置文件错误位置</li>
</ul>
<h2 id="代理"><a href="#代理" class="headerlink" title="代理"></a>代理</h2><blockquote>
<p>你们各种用户全部来访问我(nginx),我把你们的请求转给其他各个服务</p>
</blockquote>
<h3 id="正向代理-客户端配置"><a href="#正向代理-客户端配置" class="headerlink" title="正向代理(客户端配置)"></a>正向代理(客户端配置)</h3><ul>
<li>在<code>客户端</code>配置<code>代理服务器</code>，通过<code>代理服务器</code>进行互联网访问</li>
<li>如:电脑不能访问谷歌，配置 vpn，通过 vpn 代理访问<br><img src= "" data-lazy-src="/../../static/img/linux-nginx1.png" alt="图1"></li>
</ul>
<h3 id="反向代理-服务端配置"><a href="#反向代理-服务端配置" class="headerlink" title="反向代理(服务端配置)"></a>反向代理(服务端配置)</h3><ul>
<li>客户端不需要任何配置，将请求发送到反向代理服务器,由反向代理服务器去选择目标服务获取数据返回(tomcat)<br><img src= "" data-lazy-src="/../../static/img/linux-nginx2.png" alt="图2"></li>
<li>案例(实现图 6 操作)<ul>
<li>window 修改 host (无所谓)</li>
<li>访问 nginx 服务</li>
<li>nginx 配置 <code>proxy_pass http://127.0.0.1:8080;</code>,让 window 用户访问到服务器上 8080 这个服务</li>
</ul>
</li>
</ul>
<p><img src= "" data-lazy-src="/../../static/img/linux-nginx6.png" alt="图6"></p>
<h2 id="轮询"><a href="#轮询" class="headerlink" title="轮询"></a>轮询</h2><blockquote>
<p>如果有三台服务器，先不管负载均衡的权重，第一请求转服务器1，第二请求转服务器2，第三请求转服务器3，第四转1，论这给</p>
</blockquote>
<h2 id="负载-压力-均衡"><a href="#负载-压力-均衡" class="headerlink" title="负载(压力)均衡"></a>负载(压力)均衡</h2><ul>
<li>资源大的服务器多接点，小的少接点</li>
<li>场景:传统<code>发送服务器请求</code>-&gt;<code>处理数据</code>-&gt;<code>可能要操作SQL</code>-&gt;<code>返回结果</code></li>
<li>问题:信息数量、访问、数据、业务复杂度等增长造成请求<code>日益缓慢</code>，<code>并发量大</code>还容易造成服务器崩溃</li>
<li>解决方案<ul>
<li>提高服务器配置(不好)</li>
<li>负载均衡:增加<code>服务器数量</code>，将<code>请求分发</code>到各个服务器上，将原先请求<code>集中到单个服务器</code>的情况改为<code>分发到多个服务器</code>,从而将<code>负载</code>分发到不同的服务器</li>
<li>实现:请求不是直接发送到服务器,而是发送到反向代理服务器,在分发到各个不同的服务器<br><img src= "" data-lazy-src="/../../static/img/linux-nginx3.png" alt="图3"></li>
</ul>
</li>
<li>配置两步</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">`http块下`</span><br><span class="line">upstream myserver &#123; # 配置分发负载的服务器列表,三种种分配方式，位置不能变，真实环境一种就可以了</span><br><span class="line">    ip_hash;</span><br><span class="line">    server 192.168.1.104:8000 weight=5;</span><br><span class="line">    server 192.168.1.104:8080 weight=10;</span><br><span class="line">    fail;</span><br><span class="line">&#125;</span><br><span class="line">`localhost下`</span><br><span class="line">proxy_pass http://myserver; # 上面定义的名称</span><br></pre></td></tr></table></figure>

<ul>
<li>分配策略<ul>
<li>默认轮训(按顺序)</li>
<li>权重(weight)<ul>
<li>默认 1 权重越高，分配的客户端越多</li>
</ul>
</li>
<li>ip_hash<ul>
<li>每个请求按 ip 的 hash 分配，每个用固定访问一个服务器</li>
</ul>
</li>
<li>fair<ul>
<li>按照请求的响应时间分配，响应块的服务器优先分配</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="动静分离"><a href="#动静分离" class="headerlink" title="动静分离"></a>动静分离</h2><ul>
<li>概念:动态请求与静态请求通(静态资源)过不同的服务器来解析,加快解析速度，降低单个服务器的压力</li>
<li>通过 location 指定不同后缀名实现不同转发</li>
<li>expires 设置缓存过期时间，减少与服务器的交流，缓存后不会去服务器获取，只会返回 304，服务器通过对比文件最后更新时间是否发生变化，判断是否要重新下载<br><img src= "" data-lazy-src="/../../static/img/linux-nginx4.png" alt="图4"><br><img src= "" data-lazy-src="/../../static/img/linux-nginx7.png" alt="图7"></li>
</ul>
<h2 id="location-里的属性"><a href="#location-里的属性" class="headerlink" title="location 里的属性"></a>location 里的属性</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">location /img/ &#123; # 配置静态资源服务器 </span><br><span class="line">    alias /var/www/image/;</span><br><span class="line">    autoindex on; # 列出文件列表</span><br><span class="line">&#125;</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">若按照上述配置的话，则访问/img/目录里面的文件时，ningx会自动去/var/www/image/目录找文件</span></span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">location /img/ &#123;</span><br><span class="line">    root /var/www/image;</span><br><span class="line">    index index.html index.htm;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">若按照这种配置的话，则访问/img/目录下的文件时，nginx会去/var/www/image/img/目录下找文件。</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">按顺序先找index.html 如果没有再找 index.htm</span></span><br></pre></td></tr></table></figure>
<h2 id="nginx-配置高可以集群"><a href="#nginx-配置高可以集群" class="headerlink" title="nginx 配置高可以集群"></a>nginx 配置高可以集群</h2><ul>
<li>为了 nginx 挂了,程序还能正常执行</li>
<li>准备<ul>
<li>两个配置好 nginx 的服务器</li>
<li>都安装 keepalived (<code>/etc/keepalived/</code>) 配置主从服务器</li>
<li>keepalived 配置文件可以配置虚拟 ip、检测脚本路径、检测脚本中测试是否切换</li>
<li>启动两个<code>nginx</code>和<code>keepalived</code><ul>
<li>systemctl start keepalived.service</li>
</ul>
</li>
<li>ifconfig 会多次虚拟 ip</li>
</ul>
</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">/etc/keepalived/keepalived.conf</span></span><br><span class="line"></span><br><span class="line">vrrp_script chk_nginx &#123; #chk_nginx自定义</span><br><span class="line">    script &quot;/etc/keepalived/nginx_check.sh&quot; ## 检测 nginx 状态的脚本路径</span><br><span class="line">    interval 2 # 检测时间间隔</span><br><span class="line">    weight -20 # 如果条件成立，权重-20</span><br><span class="line">&#125;</span><br><span class="line">vrrp_instance VI_1 &#123; # 配置虚拟ip</span><br><span class="line">    state MASTER # 备份服务器将MASTER 改为 BACKUP</span><br><span class="line">    interface eth0 # 在使用的网卡 常用ip的那个</span><br><span class="line">    virtual_router_id 51 # 主、备用服务器的这个id必须相同</span><br><span class="line">    priority 100  # 主、备用服务器的优先级，主大、备小</span><br><span class="line">    advert_int 1 # 心跳包</span><br><span class="line">    authentication &#123;</span><br><span class="line">        auth_type PASS</span><br><span class="line">        auth_pass 1111</span><br><span class="line">    &#125;</span><br><span class="line">    track_script &#123;</span><br><span class="line">                chk_nginx ## 执行 Nginx 监控的服务</span><br><span class="line">    &#125;</span><br><span class="line">    virtual_ipaddress &#123;</span><br><span class="line">        192.168.200.16  # 虚拟ip地址</span><br><span class="line">        192.168.200.17</span><br><span class="line">        192.168.200.18</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>shell检测脚本</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line"></span><br><span class="line">A=`ps -C nginx --no-header |wc -l`</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">判断nginx是否挂掉，如果挂掉了，尝试重启</span></span><br><span class="line">if [ $A -eq 0 ];then</span><br><span class="line">    /usr/local/nginx/sbin/nginx</span><br><span class="line">    # 等待一小会再次检查nginx，如果没有启动成功，则停止keepalived，使其启动备用机</span><br><span class="line">    sleep 3</span><br><span class="line">    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then</span><br><span class="line">        killall keepalived</span><br><span class="line">    fi</span><br><span class="line">fi</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><img src= "" data-lazy-src="/../../static/img/linux-nginx8.png" alt="图8"></p>
<h3 id="原理"><a href="#原理" class="headerlink" title="原理"></a>原理</h3><h3 id="内网穿透工具"><a href="#内网穿透工具" class="headerlink" title="内网穿透工具"></a>内网穿透工具</h3><h4 id="natapp"><a href="#natapp" class="headerlink" title="natapp"></a>natapp</h4><p>1、<a target="_blank" rel="noopener" href="https://natapp.cn/">natapp</a> 注册账号、实名认证、创建免费隧道、得到一个 <strong>authtoken</strong></p>
<p>2、<a target="_blank" rel="noopener" href="https://natapp.cn/#download">下载</a>、指令传到 <code>/usr/local/natapp</code></p>
<p>3、执行 <code>./natapp -authtoken=712a064e3b816dd9lzo</code>  得到域名</p>
<p>4、后台执行 <code>nohup ./natapp -authtoken=712a064e3b816dd9lzo -log=stdout &amp;</code></p>
<hr>
<p><a target="_blank" rel="noopener" href="https://juejin.cn/post/6844903701459501070#heading-6">掘金</a><br><a target="_blank" rel="noopener" href="https://www.runoob.com/w3cnote/nginx-setup-intro.html">菜鸟</a></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://example.com">lzoxun</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://example.com/2023/07/22/linux/linux-nginx/">http://example.com/2023/07/22/linux/linux-nginx/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://example.com" target="_blank">学习笔记</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2023/07/22/linux/linux-manjaro/" title="linux-manjaro"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">linux-manjaro</div></div></a></div><div class="next-post pull-right"><a href="/2023/07/22/imd/" title=""><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">下一篇</div><div class="next_info"></div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src= "" data-lazy-src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">lzoxun</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">108</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">1</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/liaozhongxun"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/liaozhongxun" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a><a class="social-icon" href="mailto:869664233@gmail.com" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">这是我的文档</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%80%E5%90%AF-nginx"><span class="toc-number">1.</span> <span class="toc-text">开启 nginx</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%89%E8%A3%85"><span class="toc-number">1.1.</span> <span class="toc-text">安装</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE-systemctl"><span class="toc-number">1.2.</span> <span class="toc-text">配置 systemctl</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B7%BB%E5%8A%A0%E6%A8%A1%E5%9D%97"><span class="toc-number">1.3.</span> <span class="toc-text">添加模块</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#nginx%E6%BA%90"><span class="toc-number">1.4.</span> <span class="toc-text">nginx源</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84"><span class="toc-number">1.5.</span> <span class="toc-text">目录结构</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%AE%BF%E9%97%AE"><span class="toc-number">1.6.</span> <span class="toc-text">访问</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE"><span class="toc-number">2.</span> <span class="toc-text">配置</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="toc-number">2.1.</span> <span class="toc-text">基本概念</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%A3%E7%90%86"><span class="toc-number">3.</span> <span class="toc-text">代理</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%AD%A3%E5%90%91%E4%BB%A3%E7%90%86-%E5%AE%A2%E6%88%B7%E7%AB%AF%E9%85%8D%E7%BD%AE"><span class="toc-number">3.1.</span> <span class="toc-text">正向代理(客户端配置)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86-%E6%9C%8D%E5%8A%A1%E7%AB%AF%E9%85%8D%E7%BD%AE"><span class="toc-number">3.2.</span> <span class="toc-text">反向代理(服务端配置)</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BD%AE%E8%AF%A2"><span class="toc-number">4.</span> <span class="toc-text">轮询</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B4%9F%E8%BD%BD-%E5%8E%8B%E5%8A%9B-%E5%9D%87%E8%A1%A1"><span class="toc-number">5.</span> <span class="toc-text">负载(压力)均衡</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8A%A8%E9%9D%99%E5%88%86%E7%A6%BB"><span class="toc-number">6.</span> <span class="toc-text">动静分离</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#location-%E9%87%8C%E7%9A%84%E5%B1%9E%E6%80%A7"><span class="toc-number">7.</span> <span class="toc-text">location 里的属性</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#nginx-%E9%85%8D%E7%BD%AE%E9%AB%98%E5%8F%AF%E4%BB%A5%E9%9B%86%E7%BE%A4"><span class="toc-number">8.</span> <span class="toc-text">nginx 配置高可以集群</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8E%9F%E7%90%86"><span class="toc-number">8.1.</span> <span class="toc-text">原理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F%E5%B7%A5%E5%85%B7"><span class="toc-number">8.2.</span> <span class="toc-text">内网穿透工具</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#natapp"><span class="toc-number">8.2.1.</span> <span class="toc-text">natapp</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/07/22/window/win-11/" title="win 11 set">win 11 set</a><time datetime="2023-07-22T07:14:09.700Z" title="发表于 2023-07-22 15:14:09">2023-07-22</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/07/22/window/win-gitbash/" title="git bash">git bash</a><time datetime="2023-07-22T07:14:09.700Z" title="发表于 2023-07-22 15:14:09">2023-07-22</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/07/22/window/win-wsl/" title="wsl">wsl</a><time datetime="2023-07-22T07:14:09.700Z" title="发表于 2023-07-22 15:14:09">2023-07-22</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/07/22/window/win-main/" title="window">window</a><time datetime="2023-07-22T07:14:09.700Z" title="发表于 2023-07-22 15:14:09">2023-07-22</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/07/22/web/%E5%85%BC%E5%AE%B9%E9%97%AE%E9%A2%98/" title="兼容问题">兼容问题</a><time datetime="2023-07-22T07:14:09.699Z" title="发表于 2023-07-22 15:14:09">2023-07-22</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By lzoxun</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.autoSpacingPage()
  else {
    getScript('https://cdn.jsdelivr.net/npm/pangu/dist/browser/pangu.min.js')
      .then(() => {
        pangu.autoSpacingPage()
      })
  }
}

function panguInit () {
  if (false){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><div class="js-pjax"></div><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-nest.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div class="no-result" id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></div><script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"log":false,"model":{"jsonPath":"/live2dw/assets/haruto.model.json"},"display":{"position":"right","width":100,"height":200,"right":300},"mobile":{"show":true}});</script></body></html>